package top.ivansong92.example.leetcode.learning.data.struct.array.sort;

/**
 * 选择排序
 */
public class SelectSort<T extends Comparable> implements ArraySort, ObjectArraySort<T> {

    @Override
    public void sort(int[] array) {
        if (array == null || array.length < 2) {
            return;
        }
        int temp;

        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }

            if (minIndex > i) {
                temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
            }
        }

    }

    @Override
    public void sort(T[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; i++) {
                if (array[j].compareTo(array[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            if (minIndex > i) {
                T temp = array[minIndex];
                array[minIndex] = array[i];
                array[i] = temp;
            }
        }
    }
}
